#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dirent.h"
#include "IOStream.h"

#define FilePath "./NOTES/"
//#define _test_

int _readSth(FILE *, wchar_t*);
void _writeSth(FILE *, wchar_t*);

wchar_t* getFiles(){
	wchar_t * filename = NULL;
	static DIR * dir = NULL;
	struct dirent *entry;
	int namelen;
	if(dir == NULL && (dir = opendir(FilePath)) == NULL){
		checkDir();
		return ENDOFFILES;
       	}else{
		if((entry = readdir(dir)) != NULL){
			if(strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0)
				return getFiles();
			namelen = strlen(entry->d_name) + 1;
			filename = (wchar_t*) malloc(sizeof(wchar_t) * (namelen + 1));
			mbstowcs(filename, entry->d_name, namelen);
			return filename;
		}else{
			dir = NULL;
			return ENDOFFILES;
		}
	}
}

int checkDir(){
	FILE * f;
	if((f = fopen(FilePath, "r")) == NULL){
		mkdir(FilePath, S_IRWXU | S_IRWXG | S_IRWXO);
		return 1;
	}else{
		fclose(f);
		return 0;
	}
}

wchar_t * getFilePath(wchar_t * filename){
	checkDir();
	int lenOfFilename = wcslen(filename) + strlen(FilePath) + 1;
	wchar_t *filepath = (wchar_t*) malloc(lenOfFilename * sizeof(wchar_t));
	mbstowcs(filepath, FilePath, lenOfFilename - 1);
	wcscat(filepath, filename);
	return filepath;
}

char * wideToMulty(wchar_t * wide){
	int len = wcstombs(NULL, wide, 0) + 1;
	char * chs = (char*) malloc(sizeof(char) * len);
	wcstombs(chs, wide, len - 1);
	chs[len - 1] = '\0';
	return chs;
}

wchar_t * multyToWide(char * multy){
	int len = mbstowcs(NULL, multy, strlen(multy)) + 1;
	wchar_t * wcs = (wchar_t*) malloc(sizeof(wchar_t) * len);
	mbstowcs(wcs, multy, len - 1);
	return wcs;
}

wchar_t * multyToSpecifiedLenWide(char * multy, int len){
	int origin_len = mbstowcs(NULL, multy, strlen(multy)) + 1;
	if(origin_len > len) len = origin_len;
	wchar_t * wcs = (wchar_t*) malloc(sizeof(wchar_t) * len);
	mbstowcs(wcs, multy, len - 1);
	return wcs;
}

void _writeSth(FILE * file, wchar_t * sentence){
	char * sentence_multy = wideToMulty(sentence);
	fputs(sentence_multy, file);
	fputs("\n", file);
	free(sentence_multy);
}

int writeContent(ContentList* content, wchar_t * filename){
	int i;
	wchar_t * filepath_wide = getFilePath(filename);
	char * filepath_multy = wideToMulty(filepath_wide);
	FILE * file = fopen(filepath_multy, "w");
	free(filepath_wide);
	free(filepath_multy);
	for(i = 0; i < lenOfContent(content); i++){
		_writeSth(file, getContent(i, content) -> str);
	}
	fclose(file);
	return i;
}

int readToContent(wchar_t * filename, ContentList * content){
	wchar_t * filepath_wide = getFilePath(filename);
	char * filepath_multy = wideToMulty(filepath_wide);
	FILE * file = fopen(filepath_multy, "r");

	if(file == NULL){
		return 0;
	}
	
	wchar_t * sentence;
	int memsize;
	int index = 0;
	while((memsize = _readSth(file, sentence)) != 0){
		pushContentByCharArray(sentence, content, memsize);
		index ++;
	}

	fclose(file);
	free(filepath_wide);
	free(filepath_multy);
	return index;
}

int _readSth(FILE* file, wchar_t * sentence){	
	int MAX_LEN = 150;
	char * multy_sentence = (char*) malloc(sizeof(char) * (MAX_LEN + 1));
	char * old_sentence;
	int multy_len;
	int wide_len;
	if(!feof(file)){
		fgets(multy_sentence, MAX_LEN, file);
		multy_len = strlen(multy_sentence);
		while(multy_sentence[multy_len - 1] != '\n'){
			old_sentence = multy_sentence;
			MAX_LEN *= 2;
			multy_sentence = (char*) malloc(sizeof(char) * (MAX_LEN + 1));
			strcpy(multy_sentence, old_sentence);
			fgets(multy_sentence + multy_len, MAX_LEN / 2, file);
			free(old_sentence);
			multy_len = strlen(multy_sentence);
		}
		multy_sentence[multy_len - 1] = '\0';
	}else{
		free(multy_sentence);
		return 0;
	}
	sentence = multyToSpecifiedLenWide(multy_sentence, 200);
	free(multy_sentence);
	wide_len = wcslen(sentence);
	wide_len = wide_len > 200? wide_len: 200;
	return wide_len;	
}

#if defined(_test_)
int main(void){
	setlocale(LC_ALL, "");
	wchar_t * filename = NULL;
	while((filename = getFiles()) != ENDOFFILES){
		wchar_t * filepath = getFilePath(filename);
		printf("%ls\n", filepath);
		free(filename);
		free(filepath);
	}
	wchar_t test[] = L"测试，这是我的测试";
	wchar_t * sentence = malloc(sizeof(wchar_t) * 100);
	wcscpy(sentence, test);
	wchar_t fileName[] = L"名字.txt";
	wchar_t * filename_pointer = malloc(sizeof(wchar_t) * 100);
	wcscpy(filename_pointer, fileName);
	writeSth(sentence, filename_pointer);
	
	filename_pointer = malloc(sizeof(wchar_t) * 100);
	wcscpy(filename_pointer, fileName);
	printf("%ls\n", readSth(filename_pointer));

}
#endif
